package com.feihong.yw189.bluetooth.utils.djlm.newDJMLReader;

import java.util.Arrays;

/**
 * Created by Admin on 2017/4/14.
 */
class DJLM_BatchResponse
{
    byte [] m_Response = null;
    int m_nPos = 0;
    int m_nLength = 0;
    DJLM_BatchResponse(byte[] response)
    {
        m_Response = response;
        m_nPos = 0;
        m_nLength = response.length;
    }

    DJLM_BatchResponse(int nMaxLen)
    {
        m_Response = new byte[nMaxLen];
        m_nPos = 0;
        m_nLength = 0;
    }

    byte[] GetBatchResponse()
    {
        if (m_nLength == m_Response.length)
            return m_Response;
        else if (m_nLength > 0)
            return Arrays.copyOf(m_Response, m_nLength);
        return null;
    }

    static byte[] MakeSinglehResponse(byte[] Response, byte[] query, int nStep)
    {
        DJLM_BatchResponse batchResponse = new DJLM_BatchResponse(1500);
        batchResponse.AddResponse(Response, query, nStep);
        return  batchResponse.GetBatchResponse();
    }
    /*
    int AddResponse(byte[] Response)
    {
        if (m_nPos + Response.length <= m_Response.length)
        {
            System.arraycopy(Response, 0, m_Response, m_nPos, Response.length);
            m_nPos += Response.length;
            m_nLength = m_nPos;
            return m_nPos;
        }
        return -1;
    }

    byte[] GetNextResponse(int nExpectLen)
    {
        if (m_Response.length >= m_nPos + nExpectLen)
        {
            byte[] thisResponse = Arrays.copyOfRange(m_Response, m_nPos, m_nPos + nExpectLen);
            m_nPos += nExpectLen;
            return thisResponse;
        }
        return null;
    }
    */

    /*
    int AddResponse(byte[] Response)
    {
        if (m_nPos + Response.length <= m_Response.length)
        {
            if (m_nPos == 0) {
                System.arraycopy(Response, 0, m_Response, m_nPos, Response.length);
                m_nPos += Response.length;
            }
            else if (m_Response.length >= 5)
            {
                System.arraycopy(Response, 4, m_Response, m_nPos - 1, Response.length - 4);
                m_nPos += Response.length - 5;
            }
            m_nLength = m_nPos;
            return m_nPos;
        }
        return -1;
    }

    byte[] GetNextResponse(int nExpectLen)
    {
        int nCutLen = 0;
        if (m_nPos == 0)
            nCutLen = nExpectLen - 1;
        else
            nCutLen = nExpectLen - 5;

        if (m_Response.length >= m_nPos + nCutLen)
        {
            byte[] thisResponse = new byte[nExpectLen];
            if (m_nPos == 0) {
                System.arraycopy(m_Response, m_nPos, thisResponse, 0,nCutLen);
            }
            else
            {
                System.arraycopy(m_Response, m_nPos, thisResponse, 4, nCutLen);
                thisResponse[0] = 'r';
                thisResponse[1] = ' ';
                thisResponse[2] = '8';
                thisResponse[3] = '1';
            }
            m_nPos += nCutLen;
            thisResponse[nExpectLen - 1] = '\n';
            return thisResponse;
        }
        return null;
    }
    */

    int AddResponse(byte[] Response, byte[] query, int nStep)
    {
        if (Response == null)
            return -1;
        if (m_nPos + Response.length + query.length + 5 <= m_Response.length)
        {
            if (m_nPos == 0) {
                System.arraycopy(Response, 0, m_Response, m_nPos, Response.length);
                m_nPos += Response.length;

            }
            else if (m_Response.length >= 3)
            {
                m_Response[m_nPos - 1] = ',';
                System.arraycopy(Response, 2, m_Response, m_nPos, Response.length - 2);
                m_nPos += Response.length - 2;
            }
            m_Response[m_nPos-1] = ':';
            System.arraycopy(query, 2, m_Response, m_nPos, query.length-2);
            m_nPos += query.length-2;

            byte[] szStep = (String.valueOf(nStep) + "\n").getBytes();
            m_Response[m_nPos-1] = '-';
            System.arraycopy(szStep, 0, m_Response, m_nPos, szStep.length);
            m_nPos += szStep.length;

            m_nLength = m_nPos;
            return m_nPos;
        }
        return -1;
    }

    DJML_CommandItem GetNextResponse()
    {
        int nStart = m_nPos;
        int nResponseEnd = -1;
        int nQueryEnd = -1;
        int nStepEnd = -1;
        int nEnd = -1;
        int nStep = -1;
        for (int i = m_nPos; i < m_Response.length; i++)
        {
            if (m_Response[i] == ':')
            {
                nResponseEnd = i + 1;
            }
            if (m_Response[i] == '-')
            {
                nQueryEnd = i + 1;
            }
            if (m_Response[i] == ',' || m_Response[i] == '\n') {
                nEnd = i + 1;
                break;
            }
        }
        if (nEnd > nStart)
        {
            byte[] retResponese = null;
            byte[] retQuery = null;
            if (nResponseEnd < 0 && nEnd > 0)
                nResponseEnd = nEnd;
            if (nQueryEnd < 0 && nEnd > 0)
                nQueryEnd = nEnd;
            if (nStepEnd < 0 && nEnd > 0)
                nStepEnd = nEnd;

            if (nStart == 0)
                nStart = 2;
            retResponese = new byte[(nResponseEnd - nStart) + 2];
            System.arraycopy(m_Response, nStart, retResponese, 2, (nResponseEnd - nStart));
            retResponese[0] = 'r';
            retResponese[1] = ' ';
            retResponese[retResponese.length - 1] = '\n';
            if (nQueryEnd >= nResponseEnd + 1)
            {
                retQuery = new byte[nQueryEnd - 1 - nResponseEnd + 3];
                System.arraycopy(m_Response, nResponseEnd, retQuery, 2, (nQueryEnd - 1 - nResponseEnd));
                retQuery[0] = 'f';
                retQuery[1] = ' ';
                retQuery[retQuery.length - 1] = '\n';
            }
            if (nStepEnd >= nQueryEnd + 1)
            {
                byte [] szStep = new byte[nStepEnd - 1 - nQueryEnd];
                System.arraycopy(m_Response, nQueryEnd, szStep, 0, (nStepEnd - 1 - nQueryEnd));
                String strStep = new String(szStep);
                nStep = Integer.parseInt(strStep);
            }
            m_nPos = nEnd;
            ///////////////////////////////
            DJML_CommandItem commandItem = new DJML_CommandItem();
            commandItem.m_Response = retResponese;
            commandItem.m_Query = retQuery;
            commandItem.m_nStep = nStep;
            return commandItem;
        }
        return null;
    }
}
